//
//  PKMorePopoverVC.m
//  PDFKitTest
//
//  Created by Emo, on 2024/1/25.
//

#import "PKMorePopoverVC.h"


#define PKPopoverWidth               120
#define PKPopoverCellHeight          45


@interface PKMorePopoverVC () <UITableViewDelegate, UITableViewDataSource, UIPopoverPresentationControllerDelegate>

@property (nonatomic, strong) UITableView * tableView;

@property (nonatomic, strong) NSArray * dataSource;

@end

@implementation PKMorePopoverVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    PKMorePopoverModel *model1 = [[PKMorePopoverModel alloc] initWithDic:@{@"image":@"lmr_text",@"title":@"文字",@"type":@(PKPopoverSelectTypeText)}];
    PKMorePopoverModel *model2 = [[PKMorePopoverModel alloc] initWithDic:@{@"image":@"lmr_sign",@"title":@"绘制",@"type":@(PKPopoverSelectTypeDraw)}];
    self.dataSource = @[model1, model2];
    
    [self.view addSubview:self.tableView];
    self.tableView.frame = CGRectMake(0, 13, PKPopoverWidth, self.dataSource.count * PKPopoverCellHeight);
    
    self.preferredContentSize = CGSizeMake(PKPopoverWidth, self.dataSource.count * PKPopoverCellHeight);
    [self loadViewIfNeeded];
}



#pragma mark -- delegate --
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    PKMorePopoverCell *cell = [tableView dequeueReusableCellWithIdentifier:@"PKMorePopoverCell" forIndexPath:indexPath];
    PKMorePopoverModel *model = self.dataSource[indexPath.row];
    [cell setModel:model];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    [self dismissViewControllerAnimated:YES completion:nil];
    if (self.delegate && [self.delegate respondsToSelector:@selector(popoverVC:didSelectCell:)]) {
        PKMorePopoverModel *model = self.dataSource[indexPath.row];
        [self.delegate popoverVC:self didSelectCell:model.type];
    }
}


#pragma mark -- tableView --
- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:(UITableViewStylePlain)];
        _tableView.scrollEnabled = NO;
        _tableView.rowHeight = PKPopoverCellHeight;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        [_tableView registerClass:[PKMorePopoverCell class] forCellReuseIdentifier:@"PKMorePopoverCell"];
    }
    return _tableView;
}

@end



@interface PKMorePopoverCell ()

@property (nonatomic, strong) UIImageView * iconImgView;

@property (nonatomic, strong) UILabel * titleLabel;

@end

@implementation PKMorePopoverCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self setupUI];
    }
    return self;
}

- (void)setupUI {
    [self addSubview:self.iconImgView];
    [self addSubview:self.titleLabel];
}

- (void)setModel:(PKMorePopoverModel *)model {
    _model = model;
    
    self.iconImgView.image = [UIImage imageNamed:model.image];
    self.titleLabel.text = model.title;
    
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    [self.iconImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self).offset(15);
        make.centerY.mas_equalTo(self);
        make.width.height.mas_equalTo(30);
    }];
    
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.iconImgView.mas_right).offset(5);
        make.right.mas_equalTo(self.mas_right).offset(-15);
        make.centerY.mas_equalTo(self.iconImgView);
        make.height.mas_equalTo(30);
    }];
}


- (UIImageView *)iconImgView {
    if (!_iconImgView) {
        _iconImgView = [[UIImageView alloc] init];
    }
    return _iconImgView;
}

- (UILabel *)titleLabel {
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc] init];
        _titleLabel.textAlignment = NSTextAlignmentCenter;
        _titleLabel.font = kFont(17);
        _titleLabel.textColor = HexColor(@"#333333");
    }
    return _titleLabel;
}

@end



@implementation PKMorePopoverModel

- (instancetype)initWithDic:(NSDictionary *)dic {
    if (self = [super init]) {
        self.image = dic[@"image"];
        self.title = dic[@"title"];
        self.type = [dic[@"type"] integerValue];
    }
    return self;
}

@end
